package com.example.demo.mapper;

import com.example.demo.model.UserInfo;
import org.apache.ibatis.annotations.*;

import java.util.List;

@Mapper
public interface UserInfoMapper {


    @Select("select * from userinfo")
    List<UserInfo> selectAll();


    @Select("select * from userinfo where id=#{id}")
    UserInfo selectOne(Integer id);


    @Select("select * from userinfo where id=#{id123}")
    UserInfo selectOne2(@Param("id123") Integer id);


    @Insert("insert into userinfo (username,password,age,gender,phone) " +
            " values (#{username},#{password},#{age},#{gender},#{phone})")
    Integer insert(UserInfo userInfo);


    @Options(useGeneratedKeys = true,keyProperty = "id")
    @Insert("insert into userinfo (username,password,age,gender,phone) " +
            " values (#{username},#{password},#{age},#{gender},#{phone})")
    Integer insert2(UserInfo userInfo);

    /*@Insert("insert into userinfo (username,password,age,gender,phone) " +
            " values (#{username},#{password},#{age},#{gender},#{phone})")
    Integer insert3(@Param("user") UserInfo userInfo);*/

    @Insert("insert into userinfo (username,password,age,gender,phone) " +
            " values (#{user.username},#{user.password},#{user.age},#{user.gender},#{user.phone})")
    Integer insert3(@Param("user") UserInfo userInfo);


    @Delete("delete from userinfo where id=#{id}")
    Integer delete(Integer id);


    @Update("update userinfo set username = #{username} where id = #{id}")
    Integer update(UserInfo userInfo);


    /**
     * 结果映射
     * 方法一: 对字段进行重命名
     * 数据库的改为Userinfo对象
     * @return
     */

    @Select("select id, username, password, age, gender, phone," +
            " delete_flag as deleteFlag, create_time as createTime, update_time as updateTime" +
            " from userinfo")
    List<UserInfo> selectAll4();


    /**
     * 结果映射
     * 方法二: 使用注解
     * @return
     */

    @Results({
            @Result(column = "delete_flag",property = "deleteFlag"),
            @Result(column = "create_time",property = "createTime"),
            @Result(column = "update_time",property = "updateTime")
    })
    @Select("select * from userinfo")
    List<UserInfo> selectAll5();

    @Results(id = "BaseMap",value = {
            @Result(column = "delete_flag",property = "deleteFlag"),
            @Result(column = "create_time",property = "createTime"),
            @Result(column = "update_time",property = "updateTime")
    })
    @Select("select * from userinfo")
    List<UserInfo> selectAll6();


    /**
     * 结果映射
     * 方法三: 使用配置的方式, 自动转驼峰
     * @return
     */
    @Select("select * from userinfo")
    List<UserInfo> selectAll7();
}












